<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAddressStore } from '@/store'
import { storeToRefs } from 'pinia'

const router = useRouter()
const address = useAddressStore()

const { addressList } = storeToRefs(address)

const getAddressList = () => {
  address.getAddressList()
  console.log(addressList.value)
}

onMounted(() => {
  getAddressList()
})
</script>

<template>
  <div class="container">
    <van-nav-bar title="收货地址" left-arrow @click-left="router.back()" fixed placeholder/>

    <div class="address-list" v-for="item in addressList" :key="item.address_id">
      <div class="address-item">
        <div class="contacts">
          <span class="name">{{ item.name }}</span>
          <span class="phone">{{ item.phone }}</span>
        </div>

        <div class="address">
          <span class="region" v-for="(region, index) in item.region" :key="index">{{ region }}</span>
          <span class="detail">{{ item.detail }}</span>
        </div>

        <div class="line"></div>

        <div class="item-option">
          <div class="_left">
            <div class="item-radio">
              <van-radio-group class="radio" v-model="checked">
                <van-radio name="1">
                  <p class="text">默认</p>
                </van-radio>
              </van-radio-group>
            </div>
          </div>

          <div class="_right">
            <div class="events">
              <span class="event-item">
                <van-icon class="iconfont icon-edit" name="edit"></van-icon>
                <span class="title">编辑</span>
              </span>
              <span class="event-item">
                <van-icon class="iconfont icon-delete" name="delete-o"></van-icon>
                <span class="title">删除</span>
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <van-empty class="empty" v-if="addressList.length === 0" description="亲，暂无收货地址" />

    <div class="footer-fixed">
      <div class="btn-wrapper">
        <van-button class="btn-item btn-item-main" @click="router.push('/create-address')">添加新收货地址</van-button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.container {
  background-color: #f8f8f8;
  padding-bottom: 60px;
}

.address-list {
  margin: 20px auto;
  padding: 15px 30px;
  width: 94%;
  box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.05);
  border-radius: 16px;
  background: #fff;
}

.contacts {
  font-size: 14px;
  margin-bottom: 8px;

  .name {
    margin-right: 8px;
  }
}

.address {
  font-size: 14px;

  .region {
    margin-right: 8px;
  }
}

.line {
  margin: 15px 0;
  border-bottom: 1px solid #f3f3f3;
}

.item-option {
  display: flex;
  justify-content: space-between;
  height: 24px;

  // 单选框
  .item-radio {
    font-size: 14px;

    .radio {
      vertical-align: middle;
      transform: scale(0.38)
    }

    .text {
      vertical-align: middle;
    }
  }

  // 操作
  .events {
    display: flex;
    align-items: center;
    line-height: 24px;

    .event-item {
      font-size: 14px;
      margin-right: 13px;
      color: #4c4c4c;

      &:last-child {
        margin-right: 0;
      }

      .title {
        margin-left: 4px;
      }
    }
  }
}

.empty {
  padding: 160px 0
}

// 底部操作栏
.footer-fixed {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  min-height: 60px;
  z-index: 11;
  box-shadow: 0 -4px 40px 0 rgba(151, 151, 151, 0.24);
  background: #fff;

  .btn-wrapper {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 30px;
  }

  .btn-item {
    flex: 1;
    font-size: 14px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #fff;
    border-radius: 25px;
  }

  .btn-item-main {
    background: linear-gradient(to right, #f9211c, #ff6335);
  }
}
</style>

<script>
export default {
  name: 'AddressIndex'
}
</script>
